//
//  LoginViewController.m
//  XMDLearn
//
//  Created by apple on 16/4/11.
//  Copyright © 2016年 hao. All rights reserved.
//

#import "LoginViewController.h"
#import "RootViewController.h"
#import "MainViewController.h"
#import "MBProgressHUD.h"
#import "TrimWhitespace.h"
#import "DAOUser.h"
#import "RegisteViewController.h"
#import "ForgetPassWordViewController.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIView *HeadView; //导航

@property (weak, nonatomic) IBOutlet UITextField *UserNameTextField; //用户名
@property (weak, nonatomic) IBOutlet UITextField *PassWordTextField; //密码
@property (weak, nonatomic) IBOutlet UIButton *LoginButton; //登陆按钮

@property (weak, nonatomic) IBOutlet UIView *UserNameView;
@property (weak, nonatomic) IBOutlet UIView *PassWordView;

@property (weak, nonatomic) IBOutlet UIButton *IconButton;


@property (strong,nonatomic) MBProgressHUD *HUD;
@property (strong,nonatomic) DAOUser *userDAO;

@property (weak, nonatomic) IBOutlet UIButton *AutoLoginButton;
@property BOOL sure;
@property(strong,nonatomic)UIActivityIndicatorView *wait;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"登陆";
    
    self.IconButton.layer.masksToBounds = YES;
    self.IconButton.layer.cornerRadius = self.IconButton.frame.size.width/0.8;
    
    self.UserNameView.layer.borderWidth = 1.0;
    self.UserNameView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.PassWordView.layer.borderWidth = 1.0;
    self.PassWordView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    
    // 添加手势
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap1];
    
    [self headViewInit];
}

// 点击空白收回键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    
    [self.view endEditing:YES];
    
}


-(void)viewWillAppear:(BOOL)animated{
    
    NSUserDefaults *atuo = [NSUserDefaults standardUserDefaults];
    self.sure = [atuo boolForKey:@"sure"];
    
    if (self.sure) {
        [self showAdicator];
        self.UserNameTextField.text = [atuo objectForKey:@"name"];
        self.PassWordTextField.text = [atuo objectForKey:@"password"];
    }
    
    [self AutoLogin];
}

-(void)AutoLogin{
    
    if (self.sure) {
        [self.AutoLoginButton setBackgroundImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
        [self performSelector:@selector(come) withObject:nil afterDelay:3];
    } else {
        [self.AutoLoginButton setBackgroundImage:[UIImage imageNamed:@"check2"] forState:UIControlStateNormal];
    }
}

-(void)come{
    
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:self.UserNameTextField.text forKey:@"name"];
    
    // 跳转
    RootViewController *mainVC = [[RootViewController alloc]init];
    
    [self presentViewController:mainVC animated:YES completion:nil];
}

-(void)showAdicator{
    
    //设置显示大小
    self.wait = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
    
    //设置显示样式
    self.wait.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    
    //设置显示位置
    [self.wait setCenter:CGPointMake(180, 380)];
    
    //设置背景色
    self.wait.backgroundColor = [UIColor blackColor];
    self.wait.alpha = 0.5;
    [self.view addSubview:self.wait];
    
    //设置背景为圆角矩形
    self.wait.layer.cornerRadius = 10;
    self.wait.layer.masksToBounds = YES;
    
    
    //开始显示loading动画
    [self.wait startAnimating];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 导航栏
-(void)headViewInit{
    
    self.HeadView.backgroundColor = [UIColor colorWithRed:225/255.0 green:39/255.0 blue:42/255.0 alpha:1.0];

}
// 登陆按钮
- (IBAction)LoginBtn:(id)sender {
    
    if ([self validataLoginData]) {
        
        RootViewController *root  = [[RootViewController alloc] init];
      //  [self presentViewController:root animated:YES completion:nil];
        
        self.userDAO = [[DAOUser alloc]init];
        
        [self.userDAO loginWithphoneNumber:self.UserNameTextField.text passWord:self.PassWordTextField.text successHandler:^(User *user) {
            
            NSLog(@"进来了");
            [self presentViewController:root animated:YES completion:nil];
            
        } errorHandler:^(NSString *code, NSString *message) {
            [self showHUD:message];
            
        }];
    }
    
}

// 注册按钮
- (IBAction)RegisteBtn:(id)sender {
    
    RegisteViewController *registe = [[RegisteViewController alloc]init];
    [self presentViewController:registe animated:YES completion:nil];
}

// 自动登陆按钮
- (IBAction)AutoLoginBtn:(id)sender {
    
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    
    if (!self.sure) {
        [self.AutoLoginButton setBackgroundImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
        [userdefault setObject:self.UserNameTextField.text forKey:@"name"];
        [userdefault setObject:self.PassWordTextField.text forKey:@"password"];
        self.sure = YES;
    } else {
        [self.AutoLoginButton setBackgroundImage:[UIImage imageNamed:@"check2"] forState:UIControlStateNormal];
        self.sure = NO;
    }
    [userdefault setBool:self.sure forKey:@"sure"];
    
}



// 忘记密码按钮
- (IBAction)ForgetPasswordBtn:(id)sender {
    
    ForgetPassWordViewController *foreget = [[ForgetPassWordViewController alloc]init];
    [self presentViewController:foreget animated:YES completion:nil];
    
}

// 验证登陆
-(BOOL)validataLoginData{
    
    if ([TrimWhitespace isNullOrEmptyOrWhiteSpace:self.UserNameTextField.text]) {
        [self showHUD:@"请输入用户名"];
        return NO;
    }
    if ([TrimWhitespace isNullOrEmptyOrWhiteSpace:self.PassWordTextField.text]) {
        [self showHUD:@"请输入密码"];
        return NO;
    }
    if (self.PassWordTextField.text.length < 6 || self.PassWordTextField.text.length > 20) {
        
        [self showHUD:@"密码最低6位最高20位"];
        return NO;
    }
    
    return YES;
}

-(void)showHUD:(NSString*)text{
    
    self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
    
    [self.view addSubview:self.HUD];
    self.HUD.labelText = text;
    self.HUD.mode = MBProgressHUDModeText;
    
    self.HUD.yOffset = 100.0f;
    self.HUD.xOffset = 0.0f;
    
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1.5);
    } completionBlock:^{
        [self.HUD removeFromSuperview];
        self.HUD = nil;
    }];
}



@end
